package com.hoki.zj.hrm.interceptor;

import com.hoki.zj.hrm.asserts.GlobalAssert;
import feign.RequestInterceptor;
import feign.RequestTemplate;
import org.springframework.boot.jdbc.metadata.HikariDataSourcePoolMetadata;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import javax.servlet.http.HttpServletRequest;
import javax.sql.DataSource;

/**
 * Feign请求拦截器 FeignResourceInterceptor
 *  获取 TOKE
 */
@Component
public class FeignResourceInterceptor implements RequestInterceptor {

    private static final String HEADER_NAME = "Authorization";

    @Override
    public void apply(RequestTemplate requestTemplate) {
        // 1.通过请求上下文控制器获取当前请求属性
        ServletRequestAttributes requestAttributes =
                (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        // 2.判断请求不为空
        GlobalAssert.isNull(requestAttributes, "请求属性为空!");
        HttpServletRequest request = requestAttributes.getRequest();
        // 3.获取请求头中的token
        String header = request.getHeader(HEADER_NAME);
        System.out.println(header);
        // 4.将获取到的token添加到调用feign的请求中
        requestTemplate.header(HEADER_NAME,header);
    }
}
